package com.budwk.app.sqfwV2.generator.weiqingmen;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 * <p>
 * 威清门消防安全排查 SafetyCheck
 * 设置整改照片.docx
 */
public class RectifyImgPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        List<String> before = BaseGenerator.getPicturesBeforeRectification(fieldDTO);
        List<String> after = BaseGenerator.getPicturesAfterRectification(fieldDTO);
        if (before.size() > 0 || after.size() > 0) return true;
        return false;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/RectifyImg.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        List<String> before = BaseGenerator.getPicturesBeforeRectification(fieldDTO);
        List<String> after = BaseGenerator.getPicturesAfterRectification(fieldDTO);

        if (before.size() == 0) {
            paramMap.put("before_title", "");
            paramMap.put("before_pictures", new ArrayList<>());
        } else {
            //整改前
            String before_title = "附件：{} {} 整改前照片";
            before_title = StrFormatter.format(before_title, fieldDTO.getPlaceName(),
                    DateUtil.format(fieldDTO.getInspectionDate(), "yyyy-MM-dd"));
            List<Map> before_pictures = new ArrayList<>();
            List<String> before_picture = BaseGenerator.getPicturesBeforeRectification(fieldDTO);
            for (String url : before_picture) {
                if (!StrUtil.isBlank(url)) {
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("picture", PictureUtils.scaleImage(url, 240, false));
                    before_pictures.add(pictureMap);
                }
            }
            paramMap.put("before_title", before_pictures.size() == 0 ? "" : before_title);
            paramMap.put("before_pictures", before_pictures);
        }
        if (after.size() == 0) {
            paramMap.put("after_title", "");
            paramMap.put("after_pictures", new ArrayList<>());
        } else {
            //整改后
            String after_title = "附件：{} {} 整改后照片";
            String after_date = DateUtil.format(fieldDTO.getInspectionDate(), "yyyy-MM-dd");
            after_title = StrFormatter.format(after_title, fieldDTO.getPlaceName(), after_date);
            List<Map> after_pictures = new ArrayList<>();
            List<String> after_picture = BaseGenerator.getPicturesAfterRectification(fieldDTO);
            for (String url : after_picture) {
                if (!StrUtil.isBlank(url)) {
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("picture", PictureUtils.scaleImage(url, 240, false));
                    after_pictures.add(pictureMap);
                }
            }
            paramMap.put("after_title", after_pictures.size() == 0 ? "" : after_title);
            paramMap.put("after_pictures", after_pictures);
        }
    }
}
